import headerWrap from "../commonComponents/header.vue"
import noDataWrap from "../commonComponents/noMoreData.vue"
import loadingWrap from "../commonComponents/loadingInPage.vue"
import searchList from "../progressPublic/searchList.vue"
import switchTab from "../commonComponents/switchTab.vue"

export default {
  data() {
    return {
      urlType: 1,
      type: 1,
      show: true,
      switchFlag: 0,
      tabData: ["在办事宜", "办结事宜"],
      pendingPageIndex: 0,
      completePageIndex: 0,
      pendingList: [],
      completeList: [],
      detailPath: null,
      header: ""
    }
  },
  methods: {
    //urlType 2.事件管理 3.预警管理 9 专家我的任务 10 风险提示
    init() {
      this.urlType = parseInt(_methods.getUrlParam("type"));
      _methods.headerTitleChange(this.urlType);
      if (this.urlType === 9) {
        this.tabData = ["待办事宜", "已办事宜"];
      }
      setTimeout(() => {
        this.pendingListInit();
        this.completeListInit();
      }, 100);
    },
    pendingListInit() {
      let self = this;
      window.pendingList = _methods.initMeScroll("pending", {
        //刷新
        down: {
          callback: () => {
            self.getList(1, 1);
          }
        },
        //加载更多
        up: {
          warpId: "pending",
          callback: () => {
            self.getList(1, 2);
          }
        }
      });
    },
    completeListInit() {
      window.completeList = _methods.initMeScroll("complete", {
        //刷新
        down: {
          callback: () => {
            this.getList(2, 1);
          }
        },
        //加载更多
        up: {
          warpId: "complete",
          callback: () => {
            this.getList(2, 2);
          }
        }
      });
    },
    //列表
    getList(listFlag, scrollFlag) {
      /*
       listFlag：1：待处理 2：已完结
       scrollFlag：1：刷新 2：加载更多
       */
      let self = this;
      let pageKey = "page";
      let isDoneKey = "isDone";
      if (this.urlType == 9) {
        isDoneKey = "isJudged";
      }

      //列表参数
      let listKey = ["", "pendingList", "completeList"][listFlag];
      let status = ["", "1", "2"][listFlag];
      //格式化列表 list = this[listFormKey](list)
      let listFormKey = "eventListFormat";

      let ajUrl = {
        2: _ajaxUrls.eventList,
        3: _ajaxUrls.riskManage,
        9: _ajaxUrls.judged,
        10: _ajaxUrls.riskManage,
      }[this.urlType];

      let data = {
        userID: window._data.userInfo.userId,
        size: window._data.pageSize,
        timeType: 1,
        eventLevel: "",
        eventType: "",
        status,
      };
      if (this.urlType == 10) {
        data.warningLevel = 0;
      }
      //ajax获取列表数据
      let ajData = (s) => {
        _ajax.noLoading(ajUrl.url, data, (v) => {
          let data = v.result.records;
          let len = data.length;
          data = self[listFormKey](data);
          if (s === 1) {
            //刷新
            let arr = [];
            data.forEach((i) => {
              if (i && i.title) {
                arr.push(i);
              }
            });
            this[listKey] = arr;
          } else {
            //加载更多
            let arr = self[listKey];
            data.forEach((i) => {
              if (i && i.title) {
                arr.push(i);
              }
            });
            this[listKey] = arr;
          }
          window[listKey].endSuccess(len, len == window._data.pageSize);
        }, ajUrl.type);
      };
      //在办eventListFormat
      if (listFlag === 1) {
        data[isDoneKey] = 0;
        if (scrollFlag === 1) {
          //刷新列表
          data[pageKey] = 1;
          this.pendingPageIndex = 1;
          ajData(scrollFlag);
        } else {
          // 加载更多
          this.pendingPageIndex = this.pendingPageIndex + 1;
          data[pageKey] = this.pendingPageIndex;
          ajData(scrollFlag);
        }
      }
      //完结
      if (listFlag === 2) {
        data[isDoneKey] = 1;
        if (scrollFlag === 1) {
          //刷新列表
          data[pageKey] = 1;
          this.completePageIndex = 1;
          ajData(scrollFlag);
        } else {
          // 加载更多
          this.completePageIndex = this.completePageIndex + 1;
          data[pageKey] = this.completePageIndex;
          ajData(scrollFlag);
        }
      }
    },
    eventListFormat(arr) {
      return arr.map((i) => {
        i.completeIcon = "i-state-complete";
        if (i.approvalUserID == window._data.userInfo.userId) {
          i.stateClass = "i-state-pend";
        } else {
          i.stateClass = "i-state-done";
        }
        // i.dptName = i.sendDeptName;
        i.time = i.happenTime;
        if (this.urlType === 3) {
          i.iconClass = "icon-yujing";
          i.iconWrapClass = `warn${i.warningLevel}`;
        } else {
          i.iconClass = "icon-hxzfont08";
        }
        if (this.urlType == 9) {
          i.title = i.article.title;
          i.time = i.createTime;
          i.stateClass = "i-state-pend";
          i.completeIcon = "i-state-done";
        }
        return i;
      });
    },
    go_details(item) {

      // flag 1 事件管理进入详情  2 我的任务进入详情

      // listType:列表类型（1 事件管理，2 我的任务,3 预警管理）

      let type = this.urlType;

      let url = {
        2: `/riskList/riskDetail?id=${item.articleID}&listType=1&type=2&flag=1`,
        3: `/riskList/riskDetail?id=${item.articleID}&listType=1&type=3&flag=1`,
        9: `/riskList/infoDetail?id=${item.id}&type=90`,
        10: `/riskList/riskDetail?id=${item.articleID}&listType=1&type=10&flag=1`,
      }[type];

      _methods.goPath(url);
    },
    switchClick(i){
      this.switchFlag = i;
    },
  },
  components: {
    headerWrap,
    noDataWrap,
    loadingWrap,
    searchList,
    switchTab,
  },
  mounted() {
    window._methods.isNotPush().then(() => {
      this.init();
    });
    //事件列表刷新
    window._methods.indexLIstRefresh = () => {
      _methods.meScrollTriggerDownScroll(window.pendingList);
      _methods.meScrollTriggerDownScroll(window.completeList);
    };
  },
  beforeDestroy(){
    _data.bus.$off("searchVoice");
  },
}
